import { ref } from 'vue'
import { defineStore } from 'pinia'
import {
  useSubjectsService,
  useFllowService,
  useSubjectAEService,
  useSubjectSAEService,
  useSubjectCompletionService,
  useSubjectEliminateService,
  useSubjectExitService,
  useSubjectSuspendService,
  useSubjectEasiService,
  useSubjectGeneralService,
  useSubjectSmokeService,
  useSubjectDrinkService,
  useSubjectMenstruationService,
  useSubjectInducingService,
  useSubjectVitalService,
  useSubjectAllergyService,
  useSubjectDiseaseService,
  useSubjectAdFamilyService,
  useSubjectTreatmentyService,
  useSubjectSystemService,
  useSubjectOralService,
  useSubjectTopicalService,
  useSubjectOtherOneService,
  useSubjectOtherTwoService,
  useSubjectExperimentalService,
  useSubjectAdverseSercice,
  useSubjectRecrudescenceSercice,
  useSubjectFactorsSercice
} from '@/api/subject'

// instance({ url: '/patient_user', method: 'get' }).then((res) => {
//   alert('请求成功!')
//   console.log(res)
// })
export const useSubjectStore = defineStore('counter', () => {
  const id = ref(0)
  const is = ref(true)
  const list = ref([])
  const fllow = ref([])
  const AE = ref([])
  const SAE = ref([])
  const completion = ref({})
  const eliminate = ref({})
  const exit = ref({})
  const suspend = ref({})
  const easi = ref({})
  const general = ref({})
  const smoke = ref({})
  const drink = ref({})
  const menstruation = ref({})
  const inducing = ref({})
  const vital = ref({})
  const allergy = ref({})
  const disease = ref({})
  const adFamily = ref({})
  const treatment = ref({})
  const system = ref({})
  const oral = ref({})
  const topical = ref({})
  const otherOne = ref({})
  const otherTwo = ref([])

  const experimental = ref([])
  const adverse = ref([])
  const recrudescence = ref([])
  const factors = ref([])
  // 支持异步
  const setId = (data) => {
    id.value = data
  }
  const getList = async () => {
    const res = await useSubjectsService.get()
    if (res.data.data === null) {
      list.value = []
    } else {
      list.value = res.data.data
      console.log(res)
    }
  }
  const searchList = async (search) => {
    const res = await useSubjectsService.search(search)
    if (res.data.data === null) {
      list.value = []
    } else {
      list.value = res.data.data
    }
  }
  const getFllow = async () => {
    const res = await useFllowService.get()
    if (res.data.data === null) {
      list.value = []
    } else {
      list.value = res.data.data
    }
  }
  const addSubject = async (data) => {
    const res = await useSubjectsService.post(data)
    if (res.data.code === 20011) {
      ElMessage.success('添加成功')
      return true
    } else {
      ElMessage.error('添加失败')
      return false
    }
  }
  const getAE = async (id) => {
    const res = await useSubjectAEService.get(id)
    if (res.data.data === null) {
      AE.value = []
    } else {
      AE.value = res.data.data
    }
  }
  const setAE = async (data) => {
    const res = await useSubjectAEService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const addAE = async (data) => {
    const res = await useSubjectAEService.post(data)
    if (res.data.code === 20011) return true
    return false
  }
  const getSAE = async (id) => {
    const res = await useSubjectSAEService.get(id)
    if (res.data.data === null) {
      SAE.value = []
    } else {
      SAE.value = res.data.data
    }
  }
  const setSAE = async (data) => {
    const res = await useSubjectSAEService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const getCompletion = async (id) => {
    const res = await useSubjectCompletionService.get(id)
    if (res.data.data === null) {
      completion.value = {}
    } else {
      completion.value = res.data.data
    }
  }
  const setCompletion = async (data) => {
    const res = await useSubjectCompletionService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const getEliminate = async (id) => {
    const res = await useSubjectEliminateService.get(id)
    if (res.data.data === null) {
      eliminate.value = {}
    } else {
      eliminate.value = res.data.data
    }
  }
  const setEliminate = async (data) => {
    const res = await useSubjectEliminateService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const getExit = async (id) => {
    const res = await useSubjectExitService.get(id)
    if (res.data.data === null) {
      exit.value = {}
    } else {
      exit.value = res.data.data
    }
  }
  const setExit = async (data) => {
    const res = await useSubjectExitService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const getSuspend = async (id) => {
    const res = await useSubjectSuspendService.get(id)
    if (res.data.data === null) {
      suspend.value = {}
    } else {
      suspend.value = res.data.data
    }
  }
  const setSuspend = async (data) => {
    const res = await useSubjectSuspendService.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const getEasi = async (id) => {
    const res = await useSubjectEasiService.get(id)
    if (res.data.data === null) {
      easi.value = []
      await useSubjectEasiService.post({ id: id, week: 0 })
      await useSubjectEasiService.post({ id: id, week: 2 })
      await useSubjectEasiService.post({ id: id, week: 4 })
    } else {
      easi.value = res.data.data
      if (easi.value.length < 3) {
        await useSubjectEasiService.post({ id: id, week: 0 })
        await useSubjectEasiService.post({ id: id, week: 2 })
        await useSubjectEasiService.post({ id: id, week: 4 })
        if (easi.value.length < 2) easi.value[1] = { id: id, week: 2 }
        easi.value[2] = { id: id, week: 4 }
      }
    }
  }
  const setEasi = async (data) => {
    const res = await useSubjectEasiService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getGeneral = async (id) => {
    const res = await useSubjectGeneralService.get(id)
    if (res.data.data === null) {
      general.value = { id: id }
      await useSubjectGeneralService.post(general.value)
    } else {
      general.value = res.data.data
    }
  }
  const setGeneral = async (data) => {
    const res = await useSubjectGeneralService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getSmoke = async (id) => {
    const res = await useSubjectSmokeService.get(id)
    if (res.data.data === null) {
      smoke.value = { id: id }
      await useSubjectSmokeService.post(smoke.value)
    } else {
      smoke.value = res.data.data
    }
  }
  const setSmoke = async (data) => {
    const res = await useSubjectSmokeService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getDrink = async (id) => {
    const res = await useSubjectDrinkService.get(id)
    if (res.data.data === null) {
      drink.value = { id: id }
      await useSubjectDrinkService.post(drink.value)
    } else {
      drink.value = res.data.data
    }
  }
  const setDrink = async (data) => {
    const res = await useSubjectDrinkService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getMenstruation = async (id) => {
    const res = await useSubjectMenstruationService.get(id)
    if (res.data.data === null) {
      menstruation.value = { id: id }
      await useSubjectMenstruationService.post(menstruation.value)
    } else {
      menstruation.value = res.data.data
    }
  }
  const setMenstruation = async (data) => {
    const res = await useSubjectMenstruationService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getInducing = async (id) => {
    const res = await useSubjectInducingService.get(id)
    if (res.data.data === null) {
      inducing.value = { id: id }
      await useSubjectInducingService.post(inducing.value)
    } else {
      inducing.value = res.data.data
    }
  }
  const setInducing = async (data) => {
    const res = await useSubjectInducingService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getVital = async (id) => {
    const res = await useSubjectVitalService.get(id)
    if (res.data.data === null) {
      vital.value = { id: id }
      await useSubjectVitalService.post(vital.value)
    } else {
      vital.value = res.data.data
    }
  }
  const setVital = async (data) => {
    const res = await useSubjectVitalService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getAllergy = async (id) => {
    const res = await useSubjectAllergyService.get(id)
    if (res.data.data === null) {
      allergy.value = { id: id }
      await useSubjectAllergyService.post(allergy.value)
    } else {
      allergy.value = res.data.data
    }
  }
  const setAllergy = async (data) => {
    const res = await useSubjectAllergyService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getDisease = async (id) => {
    const res = await useSubjectDiseaseService.get(id)
    if (res.data.data === null) {
      disease.value = { id: id }
      await useSubjectDiseaseService.post(disease.value)
    } else {
      disease.value = res.data.data
    }
  }
  const setDisease = async (data) => {
    const res = await useSubjectDiseaseService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getAdFamily = async (id) => {
    const res = await useSubjectAdFamilyService.get(id)
    if (res.data.data === null) {
      adFamily.value = { id: id }
      await useSubjectAdFamilyService.post(adFamily.value)
    } else {
      adFamily.value = res.data.data
    }
  }
  const setAdFamily = async (data) => {
    const res = await useSubjectAdFamilyService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getTreatment = async (id) => {
    const res = await useSubjectTreatmentyService.get(id)
    if (res.data.data === null) {
      treatment.value = { id: id }
      await useSubjectTreatmentyService.post(treatment.value)
    } else {
      treatment.value = res.data.data
    }
  }
  const setTreatment = async (data) => {
    const res = await useSubjectTreatmentyService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getSystem = async (id) => {
    const res = await useSubjectSystemService.get(id)
    if (res.data.data === null) {
      system.value = { id: id }
      await useSubjectSystemService.post(system.value)
    } else {
      system.value = res.data.data
    }
  }
  const setSystem = async (data) => {
    const res = await useSubjectSystemService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getOral = async (id) => {
    const res = await useSubjectOralService.get(id)
    if (res.data.data === null) {
      oral.value = { id: id }
      await useSubjectOralService.post(oral.value)
    } else {
      oral.value = res.data.data
    }
  }
  const setOral = async (data) => {
    const res = await useSubjectOralService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getTopical = async (id) => {
    const res = await useSubjectTopicalService.get(id)
    if (res.data.data === null) {
      topical.value = { id: id }
      await useSubjectTopicalService.post(topical.value)
    } else {
      topical.value = res.data.data
    }
  }
  const setTopical = async (data) => {
    const res = await useSubjectTopicalService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getOtherOne = async (id) => {
    const res = await useSubjectOtherOneService.get(id)
    if (res.data.data === null) {
      otherOne.value = { id: id }
      await useSubjectOtherOneService.post(otherOne.value)
    } else {
      otherOne.value = res.data.data
    }
  }
  const setOtherOne = async (data) => {
    const res = await useSubjectOtherOneService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getOtherTwo = async (id) => {
    const res = await useSubjectOtherTwoService.get(id)
    if (res.data.data === null) {
      otherTwo.value = []
    } else {
      otherTwo.value = res.data.data
    }
  }
  const setOtherTwo = async (data) => {
    const res = await useSubjectOtherTwoService.put(data)
    if (res.data.code === 20031) {
      ElMessage.success('修改成功')
      return true
    } else {
      ElMessage.error('修改失败')
      return false
    }
  }
  const addOtherTwo = async (data) => {
    const res = await useSubjectOtherTwoService.post(data)
    if (res.data.code === 20011) {
      ElMessage.success('添加成功')
      return true
    } else {
      ElMessage.error('添加失败')
      return false
    }
  }
  const delOtherTwo = async (data) => {
    const res = await useSubjectOtherTwoService.delete(data)
    if (res.data.code === 20021) {
      ElMessage.success('删除成功')
      return true
    } else {
      ElMessage.error('删除失败')
      return false
    }
  }
  const getExperimental = async (id) => {
    const res = await useSubjectExperimentalService.get(id)
    if (res.data.data.length === 0) {
      await useSubjectExperimentalService.post({ id: id, week: 0 })
      await useSubjectExperimentalService.post({ id: id, week: 2 })
      await useSubjectExperimentalService.post({ id: id, week: 4 })
      getExperimental(id)
    } else {
      experimental.value = res.data.data
    }
  }
  const setExperimental = async (data) => {
    const res = await useSubjectExperimentalService.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const getAdverse = async (id) => {
    const res = await useSubjectAdverseSercice.get(id)
    if (res.data.data.length === 0) {
      await useSubjectAdverseSercice.post({ id: id, week: 2 })
      await useSubjectAdverseSercice.post({ id: id, week: 4 })
      getAdverse(id)
    } else {
      adverse.value = res.data.data
    }
  }
  const setAdverse = async (data) => {
    const res = await useSubjectAdverseSercice.put(data)
    if (res.data.code === 20031) {
      return true
    } else {
      return false
    }
  }
  const addAdverse = async (data) => {
    const res = await useSubjectAdverseSercice.post(data)
    if (res.data.code === 20011) return true
    return false
  }
  const getRecrudescence = async (id) => {
    const res = await useSubjectRecrudescenceSercice.get(id)
    recrudescence.value = res.data.data
    console.log(recrudescence.value)
  }
  const setRecrudescence = async (data) => {
    const res = await useSubjectRecrudescenceSercice.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const addRecrudescence = async (data) => {
    const res = await useSubjectRecrudescenceSercice.post(data)
    if (res.data.code === 20011) return true
    return false
  }
  const getFactors = async (id) => {
    const res = await useSubjectFactorsSercice.get(id)
    factors.value = res.data.data
  }
  const setFactors = async (data) => {
    const res = await useSubjectFactorsSercice.put(data)
    if (res.data.code === 20031) return true
    return false
  }
  const addFactors = async (data) => {
    const res = await useSubjectFactorsSercice.post(data)
    if (res.data.code === 20011) return true
    return false
  }
  return {
    is,
    id,
    setId,
    list,
    getList,
    searchList,
    fllow,
    getFllow,
    addSubject,
    AE,
    getAE,
    setAE,
    addAE,
    SAE,
    getSAE,
    setSAE,
    completion,
    getCompletion,
    setCompletion,
    eliminate,
    getEliminate,
    setEliminate,
    exit,
    getExit,
    setExit,
    suspend,
    getSuspend,
    setSuspend,
    easi,
    getEasi,
    setEasi,
    general,
    getGeneral,
    setGeneral,
    smoke,
    getSmoke,
    setSmoke,
    drink,
    getDrink,
    setDrink,
    menstruation,
    getMenstruation,
    setMenstruation,
    inducing,
    getInducing,
    setInducing,
    vital,
    getVital,
    setVital,
    allergy,
    getAllergy,
    setAllergy,
    disease,
    getDisease,
    setDisease,
    adFamily,
    getAdFamily,
    setAdFamily,
    treatment,
    getTreatment,
    setTreatment,
    system,
    getSystem,
    setSystem,
    oral,
    getOral,
    setOral,
    topical,
    getTopical,
    setTopical,
    otherOne,
    getOtherOne,
    setOtherOne,
    otherTwo,
    getOtherTwo,
    setOtherTwo,
    delOtherTwo,
    addOtherTwo,
    experimental,
    getExperimental,
    setExperimental,
    adverse,
    getAdverse,
    setAdverse,
    addAdverse,
    recrudescence,
    getRecrudescence,
    setRecrudescence,
    addRecrudescence,
    factors,
    getFactors,
    setFactors,
    addFactors
  }
})
